package org.bugjail.servlet.testcase;

import java.util.List;

import org.bugjail.data.ExploratoryTestCase;
import org.bugjail.data.ScriptedTestCase;
import org.bugjail.data.SimpleTestCase;
import org.bugjail.data.TestCase;
import org.bugjail.data.TestCaseExecution;
import org.bugjail.dataaccess.DAOFactory;
import org.bugjail.dataaccess.objects.TestCaseExecutionDAO;

public class TestCasePrinter {

	public static String retrieveTestCaseExecutionHstoryHTML(TestCase tc) {
		try {
			TestCaseExecutionDAO dao = DAOFactory.getDAOFactory()
					.getTestCaseExecutionDAO();
			List<TestCaseExecution> tcList = dao
					.retrieveExecutionsOfTestCase(tc);
			StringBuffer sb = new StringBuffer();
			if (tcList.size() > 0) {
				String lastColor = null;
				sb
						.append("<a href='TestCaseExecutionHistoryServlet?testcaseid="
								+ tc.getId() + "'>Execution History</a>");
				sb.append("<br><div class='historic'>");
				sb
						.append("<a href='TestCaseExecutionHistoryServlet?testcaseid="
								+ tc.getId()
								+ "'><table border=0 width=100% cellspacing=0 cellpadding=0><tr>");
				for (TestCaseExecution testCaseExecution : tcList) {
					if (testCaseExecution.getResult() == TestCaseExecution.PASSED) {
						if (lastColor != null)
							sb.append("<td height=20><img src=images/"
									+ lastColor
									+ "TOg.png width=100% height=20></td>");
						sb.append("<td width=4 bgcolor='#00ff00'>&nbsp;</td>");
						lastColor = "g";
					} else if (testCaseExecution.getResult() == TestCaseExecution.FAILED) {
						if (lastColor != null)
							sb.append("<td height=20><img src=images/"
									+ lastColor
									+ "TOr.png width=100% height=20></td>");
						sb.append("<td width=4 bgcolor='#ff0000'>&nbsp;</td>");
						lastColor = "r";
					} else if (testCaseExecution.getResult() == TestCaseExecution.BLOCKED) {
						if (lastColor != null)
							sb.append("<td height=20><img src=images/"
									+ lastColor
									+ "TOy.png width=100% height=20></td>");
						sb.append("<td width=4 bgcolor='#ffff00'>&nbsp;</td>");
						lastColor = "y";
					} else {
						sb.append("<td width=4 bgcolor='#ffffff'>&nbsp;</td>");
						lastColor = null;
					}
				}
				sb.append("</tr></table></a>");
				sb.append("</div><br>");
			}
			return sb.toString();
		} catch (Exception e) {
			return "";
		}
	}

	public static String retrieveTestCaseHTML(TestCase tc) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("<table width='100%'>");
			sb.append("<tr><td width='100%'><table class='hometable'>");
			sb
					.append("<th width=50>Id</th><th>Name</th><th width=50>Version</th></tr>");
			sb.append("<tr>");
			sb.append("<td width=50>" + tc.getId() + "</td>");
			sb.append("<td>" + tc.getName() + "</td>");
			sb.append("<td width=50>" + tc.getVersion() + "</td>");
			sb.append("</tr>");
			sb.append("</table></td>");
			sb.append("</tr></table>");

			sb.append("<table width='100%'>");
			sb.append("<tr><td width='100%'><table class='hometable'>");
			sb.append("<tr><th>Objective</th></tr>");
			sb.append("<tr>");
			sb.append("<td><pre>" + tc.getObjective() + "</pre></td>");
			sb.append("</tr>");
			sb.append("</table></td>");
			sb.append("</tr></table>");

			sb.append("<table width='100%'>");
			sb.append("<tr><td width='100%'><table class='hometable'>");
			sb
					.append("<tr><th>Estimated time</th><th>Severity</th><th>Type</th></tr>");
			sb.append("<tr>");
			sb.append("<td>" + tc.getEstimatedTime() + "</td>");
			sb.append("<td>" + tc.getSeverity() + "</td>");
			if (tc.getType() != null) {
				sb.append("<td>" + tc.getType().getName() + "</td>");
			} else {
				sb.append("<td>-</td>");
			}
			sb.append("</tr>");
			sb.append("</table></td>");
			sb.append("</tr></table>");

			// sb.append("<br>Status: " + tc.getStatus());
			if (tc instanceof SimpleTestCase) {
				SimpleTestCase simple = (SimpleTestCase) tc;
				sb.append(retrieveSimpleTestCase(simple));
			} else if (tc instanceof ScriptedTestCase) {
				ScriptedTestCase scripted = (ScriptedTestCase) tc;
				sb.append(retrieveScriptedTestCase(scripted));
			} else if (tc instanceof ExploratoryTestCase) {
				ExploratoryTestCase exploratory = (ExploratoryTestCase) tc;
				sb.append(retrieveExploratoryTestCase(exploratory));
			}
			return sb.toString();
		} catch (Exception e) {
			return "";
		}
	}

	private static String retrieveSimpleTestCase(SimpleTestCase tc) {
		StringBuffer sb = new StringBuffer();
		sb.append("");

		return sb.toString();
	}

	private static String retrieveScriptedTestCase(ScriptedTestCase tc) {
		return null;
	}

	private static String retrieveExploratoryTestCase(ExploratoryTestCase tc) {
		return null;
	}
}
